home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / ana11 / wm.c < prev    next >
C/C++ Source or Header  |  1993-01-15  |  8KB  |  299 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "ana.h"
  17. #include "ana_glob.h"
  18. #include "X11/Xutil.h"
  19.  
  20.  
  21. public void GetWindowGeometry( w, x, y, width, height, border )
  22.   Window  w;
  23.   Coord   *x, *y, *width, *height;
  24.   int     *border;
  25.   {
  26.     Window  root, subw;
  27.     int     d;
  28.  
  29.     XGetGeometry( display, w, &subw, x, y, width, height, border, &d );
  30.     root = RootWindowOfScreen( screen );
  31.     d = -(*border);
  32.     XTranslateCoordinates( display, w, root, d, d, x, y, &subw );
  33.   }
  34.  
  35.  
  36. public void GrabMouse( w, ev_mask, cursor )
  37.   Window         w;
  38.   unsigned long  ev_mask;
  39.   Cursor         cursor;
  40.   {
  41.    while( XGrabPointer( display, w, False, ev_mask, GrabModeAsync,
  42.      GrabModeAsync, None, cursor, CurrentTime ) != GrabSuccess );
  43.   }
  44.  
  45.  
  46. private int GetMotionEvent( w, ev, x, y )
  47.   Window  w;
  48.   XEvent  *ev;
  49.   Coord   *x, *y;
  50.   {
  51.     int           n, mask;
  52.     Coord         rx, ry;
  53.     Window        root, subw;
  54.  
  55.     for( n = XPending( display ); n != 0; n-- )
  56.       {
  57.     XNextEvent( display, ev );
  58.     if( ev->type == ButtonPress or ev->type == ButtonRelease )
  59.         return( FALSE );
  60.       }
  61.     XQueryPointer( display, w, &root, &subw, &rx, &ry, x, y, &mask );
  62.     return( TRUE );
  63.   }
  64.  
  65.  
  66. private void MoveOutline( wind, x1, y1, x2, y2 )
  67.   Window  wind;
  68.   Coord   x1, y1, x2, y2;
  69.   {
  70.     static Coord  oldx1 = 0;
  71.     static Coord  oldy1 = 0;
  72.     static Coord  oldx2 = 0;
  73.     static Coord  oldy2 = 0;
  74.     Coord         x3, x2_3, y3, y2_3;
  75.     XSegment      outline[16];
  76.     XSegment      *s;
  77.     int           nseg = 0;
  78.  
  79.     if( x1 == oldx1 and x2 == oldx2 and y1 == oldy1 and y2 == oldy2 )
  80.     return;
  81.  
  82.     s = outline;
  83.     nseg = 0;
  84.     if( oldx1 != oldx2 and oldy1 != oldy2 )
  85.       {
  86.     x3 = (oldx2 - oldx1)/3;
  87.     x2_3 = oldx1 + x3 + x3;
  88.     x3 += oldx1;
  89.     y3 = (oldy2 - oldy1)/3;
  90.     y2_3 = oldy1 + y3 + y3;
  91.     y3 += oldy1;
  92.     s[0].x1 = oldx1;  s[0].y1 = oldy1;  s[0].x2 = oldx2;  s[0].y2 = oldy1;
  93.     s[1].x1 = oldx2;  s[1].y1 = oldy1;  s[1].x2 = oldx2;  s[1].y2 = oldy2;
  94.     s[2].x1 = oldx1;  s[2].y1 = oldy2;  s[2].x2 = oldx2;  s[2].y2 = oldy2;
  95.     s[3].x1 = oldx1;  s[3].y1 = oldy1;  s[3].x2 = oldx1;  s[3].y2 = oldy2;
  96.  
  97.     s[4].x1 = oldx1;  s[4].y1 = y3;     s[4].x2 = oldx2;  s[4].y2 = y3;
  98.     s[5].x1 = oldx1;  s[5].y1 = y2_3;   s[5].x2 = oldx2;  s[5].y2 = y2_3;
  99.     s[6].x1 = x3;     s[6].y1 = oldy1;  s[6].x2 = x3;     s[6].y2 = oldy2;
  100.     s[7].x1 = x2_3;   s[7].y1 = oldy1;  s[7].x2 = x2_3;   s[7].y2 = oldy2;
  101.     s += 8;
  102.     nseg += 8;
  103.       }
  104.     if( x1 != x2 and y1 != y2 )
  105.       {
  106.     x3 = (x2 - x1)/3;
  107.     x2_3 = x1 + x3 + x3;
  108.     x3 += x1;
  109.     y3 = (y2 - y1)/3;
  110.     y2_3 = y1 + y3 + y3;
  111.     y3 += y1;
  112.     s[0].x1 = x1;     s[0].y1 = y1;     s[0].x2 = x2;     s[0].y2 = y1;
  113.     s[1].x1 = x2;     s[1].y1 = y1;     s[1].x2 = x2;     s[1].y2 = y2;
  114.     s[2].x1 = x1;     s[2].y1 = y2;     s[2].x2 = x2;     s[2].y2 = y2;
  115.     s[3].x1 = x1;     s[3].y1 = y1;     s[3].x2 = x1;     s[3].y2 = y2;
  116.  
  117.     s[4].x1 = x1;     s[4].y1 = y3;     s[4].x2 = x2;     s[4].y2 = y3;
  118.     s[5].x1 = x1;     s[5].y1 = y2_3;   s[5].x2 = x2;     s[5].y2 = y2_3;
  119.     s[6].x1 = x3;     s[6].y1 = y1;     s[6].x2 = x3;     s[6].y2 = y2;
  120.     s[7].x1 = x2_3;   s[7].y1 = y1;     s[7].x2 = x2_3;   s[7].y2 = y2;
  121.     nseg += 8;
  122.       }
  123.     if( nseg != 0 )
  124.     XDrawSegments( display, wind, gcs.invert, outline, nseg );
  125.  
  126.     oldx1 = x1;
  127.     oldx2 = x2;
  128.     oldy1 = y1;
  129.     oldy2 = y2;
  130.   }
  131.  
  132.  
  133. public void ResizeMe()
  134.   {
  135.     Window      root;
  136.     Coord       left, top, right, bot, width, height;
  137.     Coord       lim_left, lim_right, lim_top, lim_bot;
  138.     Coord       x, y, w, h, min_w, min_h, ox, oy;
  139.     int         border, d;
  140.     XEvent      ev;
  141.     XSizeHints  shint;
  142.  
  143.     GetWindowGeometry( window, &left, &top, &width, &height, &border );
  144.     border *= 2;
  145.     right = left + width + border - 1;
  146.     bot = top + height + border - 1;
  147.  
  148.     GetMinWsize( &min_w, &min_h );
  149.     min_w += border;
  150.     min_h += border;
  151.  
  152.     w = min_w / 2;
  153.     h = min_h / 2;
  154.     lim_left = left + w;
  155.     lim_right = right - w;
  156.     lim_top = top + h;
  157.     lim_bot = bot - h;
  158.  
  159.     root = RootWindowOfScreen( screen );
  160.     XGrabServer( display );
  161.     GrabMouse( root, ButtonPressMask | ButtonReleaseMask, cursors.move );
  162.  
  163.     ev.type = MotionNotify;
  164.     w = width + border;
  165.     h = height + border;
  166.     ox = left; oy = bot;
  167.     while( GetMotionEvent( root, &ev, &x, &y ) )
  168.       {
  169.     if( x < lim_left )
  170.         ox = right;
  171.     else if( x > lim_right )
  172.         ox = left;
  173.     if( y < lim_top )
  174.         oy = bot;
  175.     else if( y > lim_bot )
  176.         oy = top;
  177.  
  178.     w = x - ox;
  179.     if( w < 0 )
  180.       {
  181.         w = 1 - w;
  182.         if( w < min_w )
  183.         w = min_w;
  184.         x = ox - w + 1;
  185.       }
  186.     else
  187.       {
  188.         w += 1;
  189.         if( w < min_w )
  190.         w = min_w;
  191.         x = ox + w - 1;
  192.       }
  193.  
  194.     h = y - oy;
  195.     if( h < 0 )
  196.       {
  197.         h = 1 - h;
  198.         if( h < min_h )
  199.         h = min_h;
  200.         y = oy - h + 1;
  201.       }
  202.     else
  203.       {
  204.         h += 1;
  205.         if( h < min_h )
  206.         h = min_h;
  207.         y = oy + h - 1;
  208.       }
  209.     MoveOutline( root, ox, oy, x, y );
  210.       }
  211.     MoveOutline( root, 0, 0, 0, 0 );        /* erase outline */
  212.     XUngrabPointer( display, CurrentTime );
  213.     XUngrabServer( display );
  214.  
  215.     w -= border;
  216.     h -= border;
  217.     if( ev.type == ButtonPress or (w == width and h == height) )
  218.     return;
  219.  
  220.     if( ox < x ) x = ox;
  221.     if( oy < y ) y = oy;
  222.  
  223.     shint.x = x;
  224.     shint.y = y;
  225.     shint.width = w;
  226.     shint.height = h;
  227.     shint.max_width = shint.max_height = 16000;
  228.     shint.width_inc = shint.height_inc = 1;
  229.     GetMinWsize( &shint.min_width, &shint.min_height );
  230.     shint.flags = ( PMinSize | PMaxSize | PResizeInc | USPosition | USSize );
  231.     XSetNormalHints( display, window, &shint );
  232.     XMoveResizeWindow( display, window, x, y, w, h );  
  233.   }
  234.  
  235.  
  236. public void MoveMe( x, y )
  237.   Coord  x, y;
  238.   {
  239.     Coord       dx, dy;
  240.     Window      root;
  241.     Coord       top, left, w, h;
  242.     int         border, d;
  243.     XEvent      ev;
  244.     XSizeHints  shint;
  245.  
  246.     GetWindowGeometry( window, &left, &top, &w, &h, &border );
  247.     dx = x + border;
  248.     dy = y + border;
  249.     border *= 2;
  250.     w += border - 1;
  251.     h += border - 1;
  252.  
  253.     root = RootWindowOfScreen( screen );
  254.     XGrabServer( display );
  255.     GrabMouse( root, ButtonReleaseMask | ButtonPressMask, cursors.move );
  256.  
  257.     ev.type = MotionNotify;
  258.     x = left; y = top;
  259.     while( GetMotionEvent( root, &ev, &x, &y ) )
  260.       {
  261.     x -= dx;
  262.     y -= dy;
  263.     MoveOutline( root, x, y, x + w, y + h );
  264.       }
  265.     MoveOutline( root, 0, 0, 0, 0 );
  266.  
  267.     XUngrabPointer( display, CurrentTime );
  268.     XUngrabServer( display );
  269.  
  270.     if( ev.type == ButtonPress or (x == left and y == top) )
  271.     return;
  272.  
  273.     shint.x = x;
  274.     shint.y = y;
  275.     shint.width = w - border + 1;
  276.     shint.height = h - border + 1;
  277.     shint.max_width = shint.max_height = 16000;
  278.     shint.width_inc = shint.height_inc = 1;
  279.     GetMinWsize( &shint.min_width, &shint.min_height );
  280.     shint.flags = ( PMinSize | PMaxSize | PResizeInc | USPosition | USSize );
  281.     XSetNormalHints( display, window, &shint );
  282.     XMoveWindow( display, window, x, y );
  283.   }
  284.  
  285.  
  286. public void IconifyMe()
  287.   {
  288.     Coord  x, y, ix, iy, w, h, b;
  289.  
  290.     GetWindowGeometry( iconW, &ix, &iy, &w, &h, &b );
  291.     GetWindowGeometry( window, &x, &y, &w, &h, &b );
  292.     XDestroyWindow( display, window );
  293.     XDestroyWindow( display, iconW );
  294.     XFlush( display );
  295.     iconW = CreateIconWindow( ix, iy );
  296.     InitWindow( FALSE, IconicState, x, y, w, h, ix, iy );
  297.     XMapWindow( display, window );
  298.   }
  299.